package com.jielin.handling.util.pdfutil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ElementListener;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.jielin.handling.entity.record.RecordDetailed;
import com.jielin.handling.entity.tsuspect.TSuspec;
import com.jielin.handling.service.record.impl.RecordDetailedServiceimpl;
import com.jielin.handling.util.ConstantUtil;
import com.jielin.handling.util.DateUtil;
import com.jielin.handling.util.StringUtil;
import com.jielin.handling.util.ZipUtil;

import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Decoder;

@Slf4j
public class PDFReport {
	private static Font headfont;// 设置字体大小
	private static Font keyfont;// 设置字体大小
	private static Font textfont;// 设置字体大小
	private static Font font;// 设置字体大小

	static {
		BaseFont bfChinese;
		try {
			// bfChinese =
			// BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
			bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			// Font.BOLD：加粗
			// Font.BOLDITALIC：斜体、加粗
			// Font.DEFAULTSIZE：中划线、下划线
			// Font.ITALIC：斜体
			// Font.NORMAL：没有样式
			// Font.STRIKETHRU：中划线
			// Font.UNDEFINED：没有样式
			// Font.UNDERLINE：下划线
			headfont = new Font(bfChinese, 25, Font.BOLD);// 设置字体大小
			keyfont = new Font(bfChinese, 15, Font.UNDERLINE);// 设置字体大小
			textfont = new Font(bfChinese, 15, Font.NORMAL);// 设置字体大小
			font = new Font(bfChinese, 12, Font.NORMAL);// 设置字体大小
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void writeSimplePdf() throws Exception {
		// 1.新建document对象
		// 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
		Document document = new Document(PageSize.A4, 50, 50, 50, 50);
		// 2.建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中。
		// 创建 PdfWriter 对象 第一个参数是对文档对象的引用，第二个参数是文件的实际名称，在该名称中还会给出其输出路径。
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\ITextTest.pdf"));
		// 3.打开文档
		document.open();
		// 4.向文档中添加内容
		// 通过 com.lowagie.text.Paragraph 来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落
		document.add(new Paragraph("First page of the document."));
		document.add(new Paragraph("Some more text on the first page with different color and font type.",
				FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new BaseColor(255, 150, 200))));
		// 5.关闭文档
		document.close();
	}

	public void writeCharpter() throws Exception {
		// 新建document对象 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
		Document document = new Document(PageSize.A4, 20, 20, 20, 20);
		// 建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中。
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\\ITextTest.pdf"));
		// 打开文件
		document.open();
		// 标题
		document.add(new Paragraph("\n11111111111111111111111111111111111111"));

		document.addTitle("Hello mingri example");
		// 作者
		document.addAuthor("wolf");
		// 主题
		document.addSubject("This example explains how to add metadata.");
		document.addKeywords("iText, Hello mingri");
		document.addCreator("My program using iText");
		// document.newPage();
		// 向文档中添加内容
		document.add(new Paragraph("\n22222222222222222222222222222222222222222222222222222222222222222"));
		document.add(new Paragraph("\n"));
		document.add(new Paragraph("\n"));
		document.add(new Paragraph("\n"));
		document.add(new Paragraph("\n"));
		document.add(new Paragraph("\n"));
		document.add(new Paragraph("First page of the document.", keyfont));
		document.add(new Paragraph("First page of the document."));
		document.add(new Paragraph("First page of the document."));
		document.add(new Paragraph("First page of the document."));
		document.add(new Paragraph("Some more text on the first page with different color and font type.",
				FontFactory.getFont(FontFactory.defaultEncoding, 10, Font.BOLD, new BaseColor(0, 0, 0))));
		Paragraph title1 = new Paragraph("Chapter 1",
				FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new BaseColor(0, 0, 255)));
		// 新建章节
		Chapter chapter1 = new Chapter(title1, 1);
		chapter1.setNumberDepth(0);
		Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1",
				FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new BaseColor(255, 0, 0)));
		Section section1 = chapter1.addSection(title11);
		Paragraph someSectionText = new Paragraph("This text comes as part of section 1 of chapter 1.");
		section1.add(someSectionText);
		someSectionText = new Paragraph("Following is a 3 X 2 table.");
		section1.add(someSectionText);
		document.add(chapter1);
		// 关闭文档
		document.close();
	}

	/**
	 * @param title         文档标题(已写死 询问/讯问笔录)
	 * @param cont          文档内容(已废除改List)
	 * @param createTime    创建时间(不需要写)
	 * @param authorName    做着名字(不需要写)
	 * @param Watermarktext 水印内容(不传参数默认是杰霖科技)
	 * @param number        第几次记笔录(必写)
	 * @param leftlist      问：你叫什么名字？ 答：我叫张三。（”：“左边的内容集合）
	 * @param rightlist     问：你叫什么名字？ 答：我叫张三。（”：“右边的内容集合）
	 * @param suspec        登记的嫌疑人信息
	 * @param rd            数据库中当次笔记的详细信息
	 * @param askunit       询/讯问人的工作单位名称
	 * @param recordunit    记录人的工作单位名称
	 * @param fileString    base64字符串签字信息
	 * @throws Exception
	 */
	public String writePdf(String title, String cont, String createTime, String authorName, String Watermarktext,
			String number, List<String> leftlist, List<String> rightlist, TSuspec suspec, RecordDetailed rd,
			String askunit, String recordunit, String fileString) throws Exception {

		// 获取跟目录
		// ResourceUtils.getURL("/").getPath();
		/*
		 * Resource resource123123 = new ClassPathResource(""); File file12321312 =
		 * resource123123.getFile(); String as232222 = file12321312.getPath();
		 */
		// String as11111 = ResourceUtils.getURL("/").getPath();
		// String ab11111 = as11111+ConstantUtil.ROOT_PATH;
		// System.out.println("path:" + as11111);

		// 1.新建document对象
		// 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
		Document document = new Document(PageSize.A4, 50, 50, 50, 50);
		// 2.建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中。
		// 创建 PdfWriter 对象 第一个参数是对文档对象的引用，第二个参数是文件的实际名称，在该名称中还会给出其输出路径。
		String path = ConstantUtil.ROOT_PATH + "recordpdfs";
		File recordpdfs = new File(path);
		if (!recordpdfs.exists() && !recordpdfs.isDirectory()) {
			log.info("目录或文件不存在！");
			recordpdfs.mkdir();
		}
		// pdf文件存储路径
//		String PdfWriterpath = path + File.separator + StringUtil.generatorUUID() + "record.pdf";
		String PdfWriterpath = path + File.separator + suspec.getName() + "的笔录" + StringUtil.generateRandomFilename()
				+ ".pdf";
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(PdfWriterpath));

		// 加水印
		writer.setPageEvent(new Watermark(Watermarktext));
		// 设置第几次
		// writer.setPageEvent(new Watermark2(number));
		// 加页眉、加页数/总页数
		writer.setPageEvent(new PdfReportM1HeaderFooter("", 15, PageSize.A4));

		// 加页脚:被询问人 被讯问人
		writer.setPageEvent(new PdfReportFooter((StringUtil.isEmpty(rd.getSign()) ? "" : rd.getSign()), fileString,
				(StringUtil.isEmpty(suspec.getPersonnelType()) ? "0301" : suspec.getPersonnelType()), 15, PageSize.A4));

		// 3.打开文档
		document.open();

		// 标题
		// 创建Chunk对象，设置下划线的厚度为0.1
		Chunk text1 = new Chunk("询问/讯问笔录");
		Phrase titlep = new Phrase();
		titlep.setFont(headfont);
		titlep.add(text1);
		// 第几次
		// 创建Chunk对象，设置下划线的厚度为0.1
		Chunk underline = new Chunk("（第 ");
		Chunk underline1 = new Chunk("   " + (StringUtil.isEmpty(number) ? 1 : number) + "   ");
		underline1.setUnderline(0.1f, -1f);
		Chunk underline2 = new Chunk(" 次）");

		Phrase ph = new Phrase();
		ph.setFont(textfont);
		ph.add(underline);
		ph.add(underline1);
		ph.add(underline2);

		// 4.向文档中添加内容
		// 通过 com.lowagie.text.Paragraph 来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落
		Paragraph pt = new Paragraph();
		pt.setAlignment(1);// 设置文字居中 0靠左 1，居中 2，靠右
		pt.add(titlep);
		pt.add(ph);
		document.add(new Paragraph("\n"));
		document.add(pt);
		document.add(new Paragraph("\n"));

		// document.add(new Paragraph(cont, keyfont));
		// PdfPTable table = new PdfPTable(1);

		// 设置表格的列宽和列数
		float[] widths = { 35f, 35f, 40f, 30f, 30f, 25f, 25f, 25f, 25f, 25f, 40f, 35f, 25f, 25f, 25f, 25f, 25f, 25f,
				25f, 25f, 25f };
		// 建立一个pdf表格
		PdfPTable table = new PdfPTable(widths);

		// table.setSpacingBefore(20f);
		// 设置表格宽度为100%
		table.setWidthPercentage(100);

		PdfPCell cell = null;
		// 第一行
		cell = new PdfPCell(new Paragraph("时间", font));
		cell.setMinimumHeight(25f);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(15);
		table.addCell(cell);
		// Calendar设置时间
		Calendar c = Calendar.getInstance();
		c.setTime(rd.getStartdate() == null ? new Date() : rd.getStartdate());
		cell = new PdfPCell(new Paragraph(c.get(Calendar.YEAR) + "", font));
		// 设置垂直对齐
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		// 设置平行对齐
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("年", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(15);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph((c.get(Calendar.MONTH) + 1) + "", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("月", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(15);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph(c.get(Calendar.DAY_OF_MONTH) + "", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("日", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(15);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph(c.get(Calendar.HOUR_OF_DAY) + "", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("时", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(15);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph(c.get(Calendar.MINUTE) + "", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("分至", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(15);
		table.addCell(cell);
		Calendar c1 = Calendar.getInstance();
		c.setTime(rd.getEnddate() == null ? new Date() : rd.getEnddate());
		cell = new PdfPCell(new Paragraph(c1.get(Calendar.YEAR) + "", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("年", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(15);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph((c1.get(Calendar.MONTH) + 1) + "", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("月", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(15);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph(c1.get(Calendar.DAY_OF_MONTH) + "", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("日", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(15);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph(c1.get(Calendar.HOUR_OF_DAY) + "", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("时", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(15);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph(c1.get(Calendar.MINUTE) + "", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("分", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(15);
		table.addCell(cell);

		// 第二行
		cell = new PdfPCell(new Paragraph("地点", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setMinimumHeight(25f);
		cell.disableBorderSide(15);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph((StringUtil.isEmpty(rd.getHomename()) ? "无" : rd.getHomename()), font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(13);
		cell.setColspan(20);
		table.addCell(cell);

		// 第三行
//		Resource resource = new ClassPathResource("static" + File.separator + "13.png");
//		BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()));

//		File file = resource.getFile();
//		String Slashimagespath = file.getPath();
		String Slashimagespath = "static" + File.separator + "13.png";
		// String Slashimagespath =
		// "C:\\Users\\WIN10\\Documents\\WXWork\\1688851657162434\\Cache\\File\\2019-03\\chms\\src\\main\\resources\\static\\13.png";

		// 标题 划线询问位置
		float titlecellimagesx = document.left() + 124f;// 图片平行位置
		float titlecellimagesy = document.top() - 35f;// 图片垂直位置
		// 标题 划线讯问位置
		float titlecellimagesx2 = document.left() + 184f;// 图片平行位置

		// 第三行 划线询问位置
		float threecellimagesx = document.left() + 3f;// 图片平行位置
		float threecellimagesy = document.top() - 121f;// 图片垂直位置
		// 第三行 划线讯问位置
		float threecellimagesx2 = document.left() + 32f;// 图片平行位置

		// 第五行 划线询问位置
		float fivecellimagesx = document.left() + 16f;// 图片平行位置
		float fivecellimagesy = document.top() - 170f;// 图片垂直位置
		// 第五行 划线讯问位置
		float fivecellimagesx2 = document.left() + 44f;// 图片平行位置

		// 标题
		// (\)斜线图片
		Image titlecellimage = Image.getInstance(Slashimagespath);
		// 根据域的大小缩放图片
		titlecellimage.scaleToFit(47, 47);
		// 第三行
		// (\)斜线图片
		Image threecellimage = Image.getInstance(Slashimagespath);
		// 根据域的大小缩放图片
		threecellimage.scaleToFit(20, 20);
		// 第五行
		// (\)斜线图片
		Image fivecellimage = Image.getInstance(Slashimagespath);
		// 根据域的大小缩放图片
		fivecellimage.scaleToFit(20, 20);
		// ******判断询问 讯问划线
		if (StringUtil.isEmpty(suspec.getPersonnelType())) {
			titlecellimage.setAbsolutePosition(titlecellimagesx, titlecellimagesy);
			threecellimage.setAbsolutePosition(threecellimagesx, threecellimagesy);
			fivecellimage.setAbsolutePosition(fivecellimagesx, fivecellimagesy);
		} else if (suspec.getPersonnelType().equals("0301") || suspec.getPersonnelType().equals("0302")) {
			titlecellimage.setAbsolutePosition(titlecellimagesx, titlecellimagesy);
			threecellimage.setAbsolutePosition(threecellimagesx, threecellimagesy);
			fivecellimage.setAbsolutePosition(fivecellimagesx, fivecellimagesy);
		} else if (suspec.getPersonnelType().equals("0303") || suspec.getPersonnelType().equals("0304")) {
			titlecellimage.setAbsolutePosition(titlecellimagesx2, titlecellimagesy);
			threecellimage.setAbsolutePosition(threecellimagesx2, threecellimagesy);
			fivecellimage.setAbsolutePosition(fivecellimagesx2, fivecellimagesy);
		}
		document.add(titlecellimage);
		document.add(threecellimage);
		document.add(fivecellimage);

		cell = new PdfPCell(new Paragraph("询问/讯问人（签名）", font));
		cell.setMinimumHeight(25f);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setColspan(4);
		cell.disableBorderSide(15);
		table.addCell(cell);
		cell = new PdfPCell(
				new Paragraph((StringUtil.isEmpty(rd.getAskpolicename()) ? "无" : rd.getAskpolicename()), font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(13);
		cell.setColspan(6);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("工作单位", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(15);
		cell.setColspan(2);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph(askunit, font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(13);
		cell.setColspan(9);
		table.addCell(cell);

		// 第四行
		cell = new PdfPCell(new Paragraph("记录人（签名）", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setMinimumHeight(25f);
		cell.setColspan(3);
		cell.disableBorderSide(15);
		table.addCell(cell);
		cell = new PdfPCell(
				new Paragraph((StringUtil.isEmpty(rd.getRecordpolicename()) ? "无" : rd.getRecordpolicename()), font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(13);
		cell.setColspan(3);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("工作单位", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(15);
		cell.setColspan(3);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph(recordunit, font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(13);
		cell.setColspan(12);
		table.addCell(cell);

		// 第五行
		cell = new PdfPCell(new Paragraph("被询问/讯问人", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setMinimumHeight(25f);
		cell.setColspan(3);
		cell.disableBorderSide(15);
		table.addCell(cell);
		if (StringUtil.isEmpty(suspec.getName())) {
			suspec.setName("无");
		}
		cell = new PdfPCell(new Paragraph((suspec.getName()), font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(13);
		cell.setColspan(4);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("性别", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(15);
		cell.setColspan(2);
		table.addCell(cell);
		if (suspec.getSex() == null) {
			suspec.setSex(0);
		}
		cell = new PdfPCell(new Paragraph((suspec.getSex() == 1 ? "男" : "女"), font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		cell.setColspan(1);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("年龄", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setColspan(1);
		cell.disableBorderSide(15);
		table.addCell(cell);
		if (suspec.getAge() == null) {
			suspec.setAge(0);
		}
		cell = new PdfPCell(new Paragraph(suspec.getAge() + "", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		cell.setColspan(1);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("出生日期", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(15);
		cell.setColspan(3);
		table.addCell(cell);
		String s11 = "000000000000000000";
		if (StringUtil.isEmpty(suspec.getIdValue())) {
			log.info("没有身份证号，默认18个0");
		} else {
			s11 = suspec.getIdValue();
		}
		if (s11.length() < 18) {
			String sss = "";
			int idValuelength = 18 - s11.length();
			for (int i = 0; i < idValuelength; i++) {
				sss += "0";
			}
			s11 = s11 + sss;
		}
		String s12 = s11.substring(6, 14);
		cell = new PdfPCell(new Paragraph(
				s12.substring(0, 4) + " 年 " + s12.substring(4, 6) + " 月 " + s12.substring(6, 8) + " 日", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(13);
		cell.setColspan(6);
		table.addCell(cell);

		// 第六行
		cell = new PdfPCell(new Paragraph("身份证件种类及号码", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setMinimumHeight(25f);
		cell.setColspan(4);
		cell.disableBorderSide(15);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph(s11, font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(13);
		cell.setColspan(12);
		table.addCell(cell);
		// String path = this.getClass().getClassLoader().getResource("/").getPath();
		// String path1 = Class.getResource("/").getPath();
//		Resource resource2 = new ClassPathResource("static" + File.separator + "Checked.png");
//		Resource resource3 = new ClassPathResource("static" + File.separator + "Unchecked.png");
//		File file2 = resource2.getFile();
//		File file3 = resource3.getFile();
//		String checkedimagespath1 = file2.getPath();
//		String uncheckedimagespath2 = file3.getPath();
		String checkedimagespath1 = "static" + File.separator + "Checked.png";
		String uncheckedimagespath2 = "static" + File.separator + "Unchecked.png";
		// String checkedimagespath1 =
		// "C:\\Users\\WIN10\\Documents\\WXWork\\1688851657162434\\Cache\\File\\2019-03\\chms\\src\\main\\resources\\static\\Checked.png";
		// String uncheckedimagespath2 =
		// "C:\\Users\\WIN10\\Documents\\WXWork\\1688851657162434\\Cache\\File\\2019-03\\chms\\src\\main\\resources\\static\\Unchecked.png";

		float imagesy = document.top() - 196f;// 图片平行位置
		float leftimagesx = document.right() - 102f;// 图片垂直位置
		float rightimagesx = document.right() - 80f;// 图片垂直位置
		// 未勾选图片
		Image uncheckedimage = Image.getInstance(uncheckedimagespath2);

		// 根据域的大小缩放图片
		uncheckedimage.scaleToFit(9, 9);

		Image checkedimage = Image.getInstance(checkedimagespath1);

		// 根据域的大小缩放图片
		checkedimage.scaleToFit(9, 9);

		// ******判断是否是人大代表而设置图片位置
		if (suspec.getIsDeputy() == null) {
			suspec.setIsDeputy(0);
		}
		if (suspec.getIsDeputy() == 0 ? false : true) {
			// 设置图片绝对位置
			checkedimage.setAbsolutePosition(leftimagesx, imagesy);
			uncheckedimage.setAbsolutePosition(rightimagesx, imagesy);
		} else {
			checkedimage.setAbsolutePosition(rightimagesx, imagesy);
			uncheckedimage.setAbsolutePosition(leftimagesx, imagesy);
		}
		document.add(checkedimage);
		document.add(uncheckedimage);
		// 是 否人大代表
		// 创建Chunk对象，设置下划线的厚度为0.1
		Chunk isrenda = new Chunk("    是");

		Chunk isrenda2 = new Chunk("    否");

		// *****判断是否是人大代表设置勾选图片
		// isrenda.setUnderline(2f, 8f);// 中划线

		Chunk isrenda3 = new Chunk("人大代表");

		Phrase prenda = new Phrase();
		prenda.add(isrenda);
		prenda.add(isrenda2);
		prenda.add(isrenda3);

		Paragraph pgrenda = new Paragraph();
		pgrenda.setFont(font);
		pgrenda.add(prenda);
		cell = new PdfPCell(pgrenda);

		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(15);
		cell.setColspan(5);
		table.addCell(cell);

		// 第七行
		cell = new PdfPCell(new Paragraph("现住址", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setMinimumHeight(25f);
		cell.setColspan(2);
		cell.disableBorderSide(15);
		table.addCell(cell);
		if (StringUtil.isEmpty(suspec.getAddress())) {
			suspec.setAddress("无");
		}
		cell = new PdfPCell(new Paragraph(suspec.getAddress(), font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(13);
		cell.setColspan(11);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("联系方式", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(15);
		cell.setColspan(3);
		table.addCell(cell);
		if (StringUtil.isEmpty(suspec.getPhone())) {
			suspec.setPhone("无");
		}
		cell = new PdfPCell(new Paragraph(suspec.getPhone(), font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(13);
		cell.setColspan(5);
		table.addCell(cell);

		// 第八行
		cell = new PdfPCell(new Paragraph("户籍所在地", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setColspan(3);
		cell.setMinimumHeight(25f);
		cell.disableBorderSide(15);
		table.addCell(cell);
		if (StringUtil.isEmpty(suspec.getHousehold())) {
			suspec.setHousehold("无");
		}
		cell = new PdfPCell(new Paragraph(suspec.getHousehold(), font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(13);
		cell.setColspan(18);
		table.addCell(cell);

		// 第九行
		// （口头传唤/被扭送/自动投案的被询问/讯问人
		// 创建Chunk对象，设置下划线的厚度为0.1
		Chunk isf = new Chunk("（");
		Chunk isf2 = new Chunk("口头传唤");
		Chunk isf3 = new Chunk("/");
		Chunk isf4 = new Chunk("被扭送");
		Chunk isf5 = new Chunk("/");
		Chunk isf6 = new Chunk("自动投案");
		Chunk isf7 = new Chunk("的被");
		Chunk isf8 = new Chunk("询问");
		Chunk isf9 = new Chunk("/");
		Chunk isf10 = new Chunk("讯问");
		Chunk isf11 = new Chunk("人");
		// *****判断是否是口头传唤/被扭送/自动投案 询问/讯问人设置中划线
		if (StringUtil.isEmpty(suspec.getGotoType())) {
			suspec.setGotoType("3");
		}
		// 口头
		if (suspec.getGotoType().equals("1")) {
			isf4.setUnderline(0.1f, 5f);
			isf6.setUnderline(0.1f, 5f);
			// 扭送
		} else if (suspec.getGotoType().equals("2")) {
			isf2.setUnderline(0.1f, 5f);
			isf6.setUnderline(0.1f, 5f);
			// 投案
		} else {
			isf2.setUnderline(0.1f, 5f);
			isf4.setUnderline(0.1f, 5f);
		}

		// 询问 讯问
		/*
		 * if (false) { isf8.setUnderline(0.1f, 5f); } else { isf10.setUnderline(0.1f,
		 * 5f); }
		 */

		// ******判断询问 讯问划线
		if (StringUtil.isEmpty(suspec.getPersonnelType())) {
			isf8.setUnderline(0.1f, 5f);
		} else if (suspec.getPersonnelType().equals("0301") || suspec.getPersonnelType().equals("0302")) {
			isf8.setUnderline(0.1f, 5f);
		} else if (suspec.getPersonnelType().equals("0303") || suspec.getPersonnelType().equals("0304")) {
			isf10.setUnderline(0.1f, 5f);
		}
		Phrase pf = new Phrase();
		pf.add(isf);
		pf.add(isf2);
		pf.add(isf3);
		pf.add(isf4);
		pf.add(isf5);
		pf.add(isf6);
		pf.add(isf7);
		pf.add(isf8);
		pf.add(isf9);
		pf.add(isf10);
		pf.add(isf11);

		Paragraph pfg = new Paragraph();
		pfg.setFont(font);
		pfg.add(pf);
		cell = new PdfPCell(pfg);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setColspan(10);
		cell.setMinimumHeight(25f);
		cell.disableBorderSide(15);
		table.addCell(cell);
		// Calendar设置时间
		Calendar createddatec = Calendar.getInstance();
		createddatec.setTime(suspec.getCreateDate() == null ? new Date() : suspec.getCreateDate());
		cell = new PdfPCell(new Paragraph((createddatec.get(Calendar.MONTH) + 1) + "", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		cell.setColspan(1);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("月", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setColspan(1);
		cell.disableBorderSide(15);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph(createddatec.get(Calendar.DAY_OF_MONTH) + "", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		cell.setColspan(1);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("日", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setColspan(1);
		cell.disableBorderSide(15);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph(createddatec.get(Calendar.HOUR_OF_DAY) + "", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		cell.setColspan(1);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("时", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setColspan(1);
		cell.disableBorderSide(15);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph(createddatec.get(Calendar.MINUTE) + "", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		cell.setColspan(2);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("分到达,", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setColspan(3);
		cell.disableBorderSide(15);
		table.addCell(cell);

		// 第十行
		// Calendar设置时间
		// TODO 嫌疑人离开时间不打印，手填
//		Calendar modidydatec = Calendar.getInstance();
//		modidydatec.setTime(suspec.getModifyDate() == null ? new Date() : suspec.getModifyDate());
//		cell = new PdfPCell(new Paragraph((modidydatec.get(Calendar.MONTH) + 1) + "", font));
		cell = new PdfPCell(new Paragraph("", font));
		cell.setMinimumHeight(25f);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		cell.setColspan(1);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("月", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setColspan(1);
		cell.disableBorderSide(15);
		table.addCell(cell);
//		cell = new PdfPCell(new Paragraph(modidydatec.get(Calendar.DAY_OF_MONTH) + "", font));
		cell = new PdfPCell(new Paragraph("", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		cell.setColspan(1);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("日", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setColspan(1);
		cell.disableBorderSide(15);
		table.addCell(cell);
//		cell = new PdfPCell(new Paragraph(modidydatec.get(Calendar.HOUR_OF_DAY) + "", font));
		cell = new PdfPCell(new Paragraph("", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		cell.setColspan(1);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("时", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setColspan(1);
		cell.disableBorderSide(15);
		table.addCell(cell);
//		cell = new PdfPCell(new Paragraph(modidydatec.get(Calendar.MINUTE) + "", font));
		cell = new PdfPCell(new Paragraph("", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.disableBorderSide(13);
		cell.setColspan(1);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("分离开,本人签名", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setColspan(4);
		cell.disableBorderSide(15);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.disableBorderSide(13);
		cell.setColspan(3);
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("）。", font));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setColspan(7);
		cell.disableBorderSide(15);
		table.addCell(cell);

		// 设置表格的列宽和列数
		float[] widths1 = { 8f, 100f };
		// 建立一个pdf表格
		PdfPTable table1 = new PdfPTable(widths1);

		// table.setSpacingBefore(20f);
		// 设置表格宽度为100%
		table1.setWidthPercentage(100);

		// PdfPCell cell1 = null;

		for (int i = 0; i < leftlist.size(); i++) {

			String righttext = rightlist.get(i);
			String righttext1 = "";
			int a = righttext.length();
			// 判断（问：、答：）的右边的字符长度是否超过右侧表格宽度（能容纳的最大字符串长度）
			int rightwidth = 30;
			if (a > rightwidth) {
				// 获取行数
				int b = (a / 30) + 1;
				for (int j = 0; j < b; j++) {
					Paragraph p = null;
					if (j > 0) {
						p = new Paragraph("", textfont);
					} else {
						p = new Paragraph(leftlist.get(i), textfont);
					}
					// 非正常打印 问：1231231
					// 非正常打印 答：1231231123123123123123123131
					// 1231231123123123123123123123
					// 1234124124
					// 打印多行
					cell = new PdfPCell(p);
					// 隐藏上、左、右边框
					cell.disableBorderSide(13);
					// 设置垂直对齐
					cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
					// 设置行高
					cell.setMinimumHeight(20);
					table1.addCell(cell);
					// 判断是否是截取的最后一行
					if ((j + 1) == b) {
						righttext1 = righttext.substring(j * rightwidth, a);
					} else {
						righttext1 = righttext.substring(j * rightwidth, (j + 1) * rightwidth);
					}
					// 右边
					cell = new PdfPCell(new Paragraph(righttext1, textfont));
					// 隐藏上、左、右边框
					cell.disableBorderSide(13);
					// 设置垂直对齐
					cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
					table1.addCell(cell);
				}

			} else {
				// 正常打印 问：1231231
				// 正常打印 答：1231231
				// 打印一行
				cell = new PdfPCell(new Paragraph(leftlist.get(i), textfont));
				// 隐藏上、左、右边框
				cell.disableBorderSide(13);
				// 设置垂直对齐
				cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
				// 设置行高
				cell.setMinimumHeight(20);
				table1.addCell(cell);

				cell = new PdfPCell(new Paragraph(rightlist.get(i), textfont));
				// 隐藏上、左、右边框
				cell.disableBorderSide(13);
				// 设置垂直对齐
				cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
				table1.addCell(cell);
			}

		}
		// 默认东西
		document.add(table);
		document.add(new Paragraph("\n"));
		// 笔录内容
		document.add(table1);
		// 5.关闭文档
		document.close();

		return PdfWriterpath;
	}

	public static void main(String[] args) throws Exception {
		String s = "<p>问：你叫什么名字？</p><p>答：我叫张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p><p>问：你叫什么名字？</p><p>答：我叫张三。</p><p>问：你家里几口人？</p><p>答：算上我4个人。</p>";
		String[] strs = s.split("<p>");
		StringBuffer zzz = new StringBuffer();
		List<String> leftlist = new ArrayList<>();
		List<String> rightlist = new ArrayList<>();
		for (int i = 1, len = strs.length; i < len; i++) {
			String[] strs1 = strs[i].split("</p>");
			String row = strs1[0].toString();

			int i1 = row.indexOf("：");
			String one1 = row.substring(0, i1 + 1);
			String two1 = row.substring(i1 + 1, row.length());
			// if(row.length() < l) {
			// row += (l-row.length())*"";
			// }
			// System.out.println(String.format("%-" + l + "s", row));
			// System.out.println(row.length());
			// zzz.append(PDFReport.formatStr(row, 33) + "\r\n");
			zzz.append(row + "\r\n");
			leftlist.add(one1);
			rightlist.add(two1);
		}

		System.out.println("begin");
		PDFReport ppt = new PDFReport();

		TSuspec suspec = new TSuspec();
		suspec.setName("张三");
		suspec.setAge(25);
		suspec.setSex(0);
		suspec.setIsDeputy(0);
		suspec.setIdValue("362202199305235510");
		suspec.setPhone("15377074555");
		suspec.setAddress("湖北省武汉市青山区团结大道仁和路老岸88号");
		suspec.setHousehold("湖北省武汉市江岸区88号");
		suspec.setGotoType("1");// 口头传唤
		suspec.setPersonnelType("0304");// 人员类别 0301:犯罪嫌疑人 0302:违法行为人 0303:证人 0304:受害人
		// 嫌疑人到达时间
		suspec.setCreateDate(new Date());
		suspec.setModifyDate(new Date());
		RecordDetailed rd = new RecordDetailed();
		rd.setStartdate(new Date());
		rd.setEnddate(new Date());
		rd.setAskpolicename("王小明");
		rd.setHomename("1号询问室");
		rd.setRecordpolicename("王晓红");
		// ppt.writePdf("询问/讯问笔录", zzz.toString(), null, null, "杰霖科技", "2", leftlist,
		// rightlist, suspec, rd, "洪山派出所", "洪山派出所");
		String path = ppt.writePdf(null, null, null, null, "杰霖科技", "2", leftlist, rightlist, suspec, rd, "洪山派出所",
				"洪山派出所", "");
		// ppt.addWaterMark("D:\\ITextTest.pdf", "D:\\ITextTest1.pdf", "万日", 400, 400,
		// "", 100, 100);

		// ppt.addWaterMark("D:\\ITextTest.pdf", "D:\\ITextTest1.pdf", "万日", 500, 980);
		System.out.println("end" + path);

	}

	public void addWaterMark(String srcFile, String destFile, String text, int textWidth, int textHeight,
			String imgFile, int imgWidth, int imgHeight) throws IOException, DocumentException {

		// 待加水印的文件
		PdfReader reader = new PdfReader(srcFile);
		// 加完水印的文件
		PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile));

		int total = reader.getNumberOfPages() + 1;
		PdfContentByte content;

		BaseFont font = BaseFont.createFont("C:/Windows/Fonts/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
		// BaseFont base2 = BaseFont.createFont(BaseFont.HELVETICA,
		// BaseFont.WINANSI, BaseFont.EMBEDDED);
		// 水印文字
		String waterText = text;
		Image image = null;
		if (!StringUtils.isBlank(imgFile)) {
			image = Image.getInstance(imgFile);
			image.setAbsolutePosition(imgWidth, imgHeight);
			// 设置图片的显示大小
			image.scaleToFit(100, 125);
		}
		int j = waterText.length(); // 文字长度
		char c = 0;
		int high = 0;// 高度
		// 循环对每页插入水印
		for (int i = 1; i < total; i++) {
			// 水印的起始
			high = 50;
			// 水印在之前文本之上
			content = stamper.getOverContent(i);
			if (image != null) {
				content.addImage(image);
			}

			if (!StringUtils.isBlank(text)) {
				// 开始
				content.beginText();
				// 设置颜色 默认为蓝色 BaseColor.GRAY灰色 BaseColor.LIGHT_GRAY浅灰色
				content.setColorFill(BaseColor.LIGHT_GRAY);
				// 设置字体及字号
				content.setFontAndSize(font, 24);
				// 设置起始位置
				content.setTextMatrix(400, 400);
				// 开始写入水印
				content.showTextAligned(Element.ALIGN_LEFT, text, textWidth, textHeight, 45);

				Watermark wm = new Watermark();

				content.endText();
			}
		}
		stamper.close();
	}

	/**
	 * 生成指定长度字符串，不足位右补空格
	 * 
	 * @param str
	 * @param length
	 * @return
	 */
	private static String formatStr(String str, int length) {
		int strLen;
		if (str == null) {
			strLen = 0;
		} else {
			strLen = str.length();
		}

		if (strLen == length) {
			return str;
		} else if (strLen < length) {
			int temp = length - strLen;
			String tem = "";
			for (int i = 0; i < temp; i++) {
				tem = tem + " ";
			}
			return str + tem;
		} else {
			return str.substring(0, length);
		}
	}

	/**
	 * 
	 * 【功能描述：添加图片和文字水印】 【功能详细描述：功能详细描述】
	 * 
	 * @param srcFile    待加水印文件
	 * @param destFile   加水印后存放地址
	 * @param text       加水印的文本内容
	 * @param textWidth  文字横坐标
	 * @param textHeight 文字纵坐标
	 * @throws Exception
	 */
	public void addWaterMark(String srcFile, String destFile, String text, int textWidth, int textHeight)
			throws Exception {
		// 待加水印的文件
		PdfReader reader = new PdfReader(srcFile);
		// 加完水印的文件
		PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile));
		int total = reader.getNumberOfPages() + 1;
		PdfContentByte content;
		// 设置字体
		BaseFont font = BaseFont.createFont();
		// 循环对每页插入水印
		for (int i = 1; i < total; i++) {
			// 水印的起始
			content = stamper.getUnderContent(i);
			// 开始
			content.beginText();
			// 设置颜色 默认为蓝色
			content.setColorFill(BaseColor.BLUE);
			// content.setColorFill(Color.GRAY);
			// 设置字体及字号
			content.setFontAndSize(font, 38);
			// 设置起始位置
			content.setTextMatrix(400, 880);
			// content.setTextMatrix(textWidth, textHeight);
			// 开始写入水印
			content.showTextAligned(Element.ALIGN_LEFT, text, textWidth, textHeight, 45);
			content.endText();
		}
		stamper.close();
	}

}
